﻿#include "widgetpicturegallery.h"
#include "Delegate/picturedelegate.h"
#include "Public/appsignal.h"

#include <QGridLayout>
#include <QImageReader>
#include <QStandardPaths>
#include <QDir>
#include <QLabel>

// test
#include <QDebug>

WidgetPictureGallery::WidgetPictureGallery(QWidget *parent)
    : QWidget{parent}
{
    init();
}

void WidgetPictureGallery::init()
{
    QFont font("Microsoft YaHei", 9);
    QFontMetrics metrics(font);
    int margin = metrics.height() * 0.5;

    QWidget *widgetBase = new QWidget(this);
    widgetBase->setFocusPolicy(Qt::NoFocus);
    widgetBase->setObjectName("widgetPictureGalleryBase");

    // 布局
    QGridLayout *layout = new QGridLayout(this);
    layout->addWidget(widgetBase);
    layout->setContentsMargins(0, 0, 0, 0);
    setLayout(layout);

    // 空项目提示
    mLabelEmpty = new QLabel(widgetBase);
    mLabelEmpty->setText("已提取所有图像");
    mLabelEmpty->setFocusPolicy(Qt::NoFocus);
    mLabelEmpty->setAlignment(Qt::AlignCenter);
    mLabelEmpty->setObjectName("lbEmptyImage");
    mLabelEmpty->setVisible(true);

    QGridLayout *layoutWidgetBase = new QGridLayout(widgetBase);
    layoutWidgetBase->addWidget(&mPictureView);
    layoutWidgetBase->addWidget(mLabelEmpty);
    layoutWidgetBase->setContentsMargins(margin, margin, margin, margin);
    widgetBase->setLayout(layoutWidgetBase);
    mPictureView.setVisible(false);

    // 视图
    mPictureView.setObjectName("lvPictureGallery");
    mPictureView.setItemDelegate(new PictureDelegate(this));
    mPictureView.setFlow(QListView::LeftToRight);
    mPictureView.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    mPictureView.setVerticalScrollMode(QListView::ScrollPerPixel);
    mPictureView.setHorizontalScrollMode(QListView::ScrollPerPixel);
    mPictureView.setMovement(QListView::Free);
    mPictureView.setDragEnabled(false);
    mPictureView.setEditTriggers(QListView::NoEditTriggers);
    mPictureView.setFocusPolicy(Qt::NoFocus);
    mPictureView.setModel(&mPictureModel);

    // 信号
    connect(AppSignal::getInstance(), &AppSignal::sgl_add_new_graphics_item_status, this, &WidgetPictureGallery::slot_add_new_graphics_item_status);
    connect(AppSignal::getInstance(), &AppSignal::sgl_revert_graphics_item_to_list, this, &WidgetPictureGallery::slot_revert_graphics_item_to_list);

    // test
    //open("C:\\Users\\admin\\Project\\PictureAssembly\\output\\newpic\\MV-CA023-10GM (00E69416202)");
    //open("C:\\Users\\admin\\Project\\PictureAssembly\\output\\newpic\\little");
}

void WidgetPictureGallery::open(const QString &path)
{
    // 由于未分组，新的图像会覆盖旧的，导致旧的看不到，这里就没地要留着了
    // 清理列表
    mPictureModel.clear();
    mMatchingItemFlag = false;

    mLabelEmpty->setVisible(true);
    mPictureView.setVisible(false);

    // 发送信号，清理图形项
    emit AppSignal::getInstance()->sgl_del_all_graphics_item();

    // 循环读取图片
    auto fileInfoList = QDir(path).entryInfoList(QStringList{"*.bmp", "*.raw", "*.png", "*.jpg"});
    if (fileInfoList.isEmpty()) return;

    mLabelEmpty->setVisible(false);
    mPictureView.setVisible(true);

    update();

    QImageReader reader;
    // 添加到列表
    for (auto &info : fileInfoList)
    {
        reader.setFileName(info.absoluteFilePath());
        QStandardItem *item = new QStandardItem(info.absoluteFilePath());
        item->setData(mPictureModel.rowCount() + 1, Qt::UserRole + 1);
        mPictureModel.appendRow(item);
    }
}

void WidgetPictureGallery::sendPreviousOne()
{
    qDebug() << "WidgetPictureGallery::sendPreviousOne " << mMatchingItemFlag;
    if (mMatchingItemFlag) return;
    mMatchingItemFlag = true;

    // 这里可以控制从哪个开始拼接
    QStandardItem *item = mPictureModel.itemFromIndex(mPictureModel.index(0, 0));
    if (nullptr == item)
    {
        mMatchingItemFlag = false;
        return;
    }

    // qDebug() << "item " << item->text();
    emit AppSignal::getInstance()->sgl_add_new_picture(item->text(), item->data(Qt::UserRole + 1).toInt());
}

void WidgetPictureGallery::slot_add_new_graphics_item_status(int index, bool status)
{
    qDebug() << "WidgetPictureGallery::slot_add_new_graphics_item_status " << index << status;
    mMatchingItemFlag = false;

    if (!status)
    {
        // 提示一下，或者做一点处理
    }

    QModelIndexList list = mPictureModel.match(mPictureModel.index(0, 0), Qt::UserRole + 1, QString::number(index), 1, Qt::MatchExactly);
    if (list.size() != 1) return;

    mPictureModel.removeRow(list.at(0).row());

    qDebug() << "list.at(0).row() " << list.at(0).row();

    mPictureView.setVisible(mPictureModel.rowCount() > 0);
    mLabelEmpty->setVisible(mPictureModel.rowCount() <= 0);

    update();
}

void WidgetPictureGallery::slot_revert_graphics_item_to_list(const QString &path, int index)
{
    mLabelEmpty->setVisible(false);
    mPictureView.setVisible(true);

    QStandardItem *item = new QStandardItem(path);
    item->setData(index, Qt::UserRole + 1);
    mPictureModel.appendRow(item);

    // 重排序
    mPictureModel.sort(0);
}
